/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Hangman;

import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.metamodel.Metamodel;

/**
 *
 * @author stuartsealfon
 */
@Stateless
public class UsersFacade extends AbstractFacade<Users> implements UsersFacadeRemote {
    //@PersistenceContext(unitName = "LMSProject-ejbPU")
    @Override
    protected EntityManager getEntityManager() {
        return new EntityManager() {

            @Override
            public void persist(Object entity) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> T merge(T entity) {
                return entity;
            }

            @Override
            public void remove(Object entity) {
                MySQLAccessBean access = new MySQLAccessBean();
                access.remove(entity.toString());
            }

            @Override
            public <T> T find(Class<T> entityClass, Object primaryKey) {
                try {
                    //Finds an element in the database.
                    MySQLAccessBean access = new MySQLAccessBean();
                    //System.out.println("1");
                    if (access.isMember(primaryKey.toString())) {
                        UserBeanRemote u = new UserBean();
                        //System.out.println("2");
                        u.setInfo(primaryKey.toString(), access.isValidPassword(primaryKey.toString()));
                        System.out.println("3");
                        Users us = new Users(u.getUsername(), u.getPassword());
                        System.out.println("4");
                        System.out.println("(((" + us + ")))");
                        System.out.println("5");
                        //us = new Users();
                        return (T) us;
                    }
                } catch (Exception ex) {
                    System.out.println("6");
                    Logger.getLogger(UsersFacade.class.getName()).log(Level.SEVERE, null, ex);
                }
                return null;
            }

            @Override
            public <T> T find(Class<T> entityClass, Object primaryKey, Map<String, Object> properties) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode, Map<String, Object> properties) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> T getReference(Class<T> entityClass, Object primaryKey) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void flush() {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void setFlushMode(FlushModeType flushMode) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public FlushModeType getFlushMode() {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void lock(Object entity, LockModeType lockMode) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void lock(Object entity, LockModeType lockMode, Map<String, Object> properties) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void refresh(Object entity) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void refresh(Object entity, Map<String, Object> properties) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void refresh(Object entity, LockModeType lockMode) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void refresh(Object entity, LockModeType lockMode, Map<String, Object> properties) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void clear() {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void detach(Object entity) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public boolean contains(Object entity) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public LockModeType getLockMode(Object entity) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void setProperty(String propertyName, Object value) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public Map<String, Object> getProperties() {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public Query createQuery(String qlString) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public Query createNamedQuery(String name) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public Query createNativeQuery(String sqlString) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public Query createNativeQuery(String sqlString, Class resultClass) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public Query createNativeQuery(String sqlString, String resultSetMapping) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void joinTransaction() {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> T unwrap(Class<T> cls) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public Object getDelegate() {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void close() {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public boolean isOpen() {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public EntityTransaction getTransaction() {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public EntityManagerFactory getEntityManagerFactory() {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public CriteriaBuilder getCriteriaBuilder() {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public Metamodel getMetamodel() {
                throw new UnsupportedOperationException("Not supported yet.");
            }

        };
    }

    public UsersFacade() {
        super(Users.class);
    }

    @Override
    public void register(String userID, String password) {
        super.register(userID, password);
    }

    @Override
    public void remove(String userID) {
        getEntityManager().remove(userID);
    }

}
